package com.huanwei.cc.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.ocr.ui.camera.CameraActivity;
import com.huanwei.cc.R;
import com.huanwei.cc.base.BaseActivity;
import com.huanwei.cc.utils.FileUtil2;
import com.huanwei.cc.utils.SpfUtils;
import com.huanwei.mylab.Me_Label_Activity;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.style.cityjd.JDCityPicker;

/**
 * Created by ADM on 2017/12/2.
 * 我的信息
 */

public class Me_data_activity extends BaseActivity implements View.OnClickListener, PopupWindow.OnDismissListener {
    private PopupWindow popupWindow;
    private int navigationHeight;
    private TextView Gender;
    private LinearLayout me_Gender, me_Label;

    @Override
    public int setLayoutId() {
        int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        navigationHeight = getResources().getDimensionPixelSize(resourceId) - 80;
        return R.layout.me_medata;
    }

    @Override
    public void initViewsAndEvents() {
        findID();


    }

    private static final int REQUEST_CODE_GENERAL_BASIC = 106;

    public void start() {
        Intent intent = new Intent(getApplication(), CameraActivity.class);
        intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                FileUtil2.getSaveFile(getApplication()).getAbsolutePath());
        intent.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL);
        startActivityForResult(intent, REQUEST_CODE_GENERAL_BASIC);
    }
    ImageView imageView;
    RelativeLayout xiugai;
    LinearLayout dizhixuanze;
    TextView dizhi,me_title;
    private void findID() {
        String name = (String) SpfUtils.get(this, "name", "");
        me_Gender = (LinearLayout) findViewById(R.id.me_Gender);//性别
        Gender = (TextView) findViewById(R.id.Gender);//性别
        dizhi = (TextView) findViewById(R.id.dizhi);//性别
        me_title = (TextView) findViewById(R.id.me_title);//性别
        me_Gender.setOnClickListener(this);
        me_Label = (LinearLayout) findViewById(R.id.me_Label);//我的标签

        imageView = (ImageView) findViewById(R.id.touxiang);//我的标签

        xiugai = (RelativeLayout) findViewById(R.id.xiugai);//性别
        dizhixuanze = (LinearLayout) findViewById(R.id.dizhixuanze);//性别
        dizhixuanze.setOnClickListener(this);
        xiugai.setOnClickListener(this);
        me_Label.setOnClickListener(this);
        me_title.setText(name);


    }

    private Bitmap loadedBitmap;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        // 识别成功回调，通用文字识别
        if (requestCode == REQUEST_CODE_GENERAL_BASIC && resultCode == Activity.RESULT_OK) {
            Log.e("888", "图片地址：" + FileUtil2.getSaveFile(getApplicationContext()).getAbsolutePath());

            if ("".equals(FileUtil2.getSaveFile(getApplicationContext()).getAbsolutePath())) {
                loadedBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
            } else {
                //ImageLoader.getInstance(3, ImageLoader.Type.LIFO).loadImage(loadImagePath, mImageView);
                        /*下面这种方式可以加载原图，但是尺寸大了之后影响几何变换，下一个实验再用这种方法*/
                loadedBitmap = BitmapFactory.decodeFile(FileUtil2.getSaveFile(getApplicationContext()).getAbsolutePath());
            }
            imageView.setImageBitmap(loadedBitmap);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.me_Gender:
                openPopupWindow(view);
                break;
            case R.id.tv_pick_phone:
                Gender.setText("女");
                popupWindow.dismiss();
                break;
            case R.id.xiugai:
                start();
                break;

            case R.id.tv_pick_zone:
                Gender.setText("男");
                popupWindow.dismiss();
                break;
            case R.id.tv_cancel:
                popupWindow.dismiss();
                break;
            case R.id.dizhixuanze:
                JDCityPicker cityPicker = new JDCityPicker();
                cityPicker.init(this);
                cityPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
                    @Override
                    public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
//                        resultV.setText("城市选择结果：\n" + province.getName() + "(" + province.getId() + ")\n"
//                                + city.getName() + "(" + city.getId() + ")\n"
//                                + district.getName() + "(" + district.getId() + ")");
                        dizhi.setText( province.getName() + city.getName()+district.getName() );
                    }

                    @Override
                    public void onCancel() {
                    }
                });
                cityPicker.showCityPicker();
                break;




        }


    }

    private void openPopupWindow(View v) {
        //防止重复按按钮
        if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }
        //设置PopupWindow的View
        View view = LayoutInflater.from(this).inflate(R.layout.me_popupwindow_gender, null);
        popupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        //设置背景,这个没什么效果，不添加会报错
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //设置点击弹窗外隐藏自身
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        //设置动画
        popupWindow.setAnimationStyle(R.style.PopupWindow);
        //设置位置
        popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, navigationHeight);
        //设置消失监听
        popupWindow.setOnDismissListener(this);
        //设置PopupWindow的View点击事件
        setOnPopupViewClick(view);
        //设置背景色
        setBackgroundAlpha(0.5f);
    }

    private void setOnPopupViewClick(View view) {
        TextView tv_pick_phone, tv_pick_zone, tv_cancel;
        tv_pick_phone = (TextView) view.findViewById(R.id.tv_pick_phone);
        tv_pick_zone = (TextView) view.findViewById(R.id.tv_pick_zone);
        tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_pick_phone.setOnClickListener(this);
        tv_pick_zone.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
    }

    //设置屏幕背景透明效果
    public void setBackgroundAlpha(float alpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = alpha;
        getWindow().setAttributes(lp);
    }

    @Override
    public void onDismiss() {
        setBackgroundAlpha(1);
    }
}